//
//  NetworkTask.swift
//
//
//  Created by wumin on 15/12/28.
//
//


import Foundation
import SwiftyJSON

public enum HttpMethod: String {
  case OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT
}

let NetworkCoreDebug = true


class NetworkConfig {
  // 灰度测试
  static var baseURL: String = "http://10.10.3.242:9000"
  
  static var reachability:Reachability? = nil
  
  static func configNetwork(WithBaseURL url:String){
    self.baseURL = url
    self.startNotifier(hostName: self.baseURL)
  }
  
  static func configNetwork(WithProxy proxy:NetworkProxyProtocol){
    NetworkHelper.shareInstance.configNetworkProxy(proxy)
  }
  
  
  static func isReachable()->Bool {
    
    guard let reachability = self.reachability else {
      return false
    }
    
    return reachability.isReachable
    
  }
  
  
  static func startNotifier(hostName:String){
    
    guard self.reachability == nil else {
      return
    }
    
    self.reachability = Reachability(hostname: hostName)
    
    self.reachability?.whenReachable = {
      reachability in
    
      if reachability.isReachableViaWWAN {
        TXLogWarn("建立连接__WWAN")
        ReachabilitySource.notifyAll(type: ReachabilityType.mobile)
      }else if reachability.isReachableViaWiFi {
        TXLogWarn("建立连接__WIFI")
        ReachabilitySource.notifyAll(type: ReachabilityType.wifi)
      }else if reachability.isReachable {
        TXLogWarn("建立连接__")
        ReachabilitySource.notifyAll(type: ReachabilityType.reachable)
      }
      
    }
    
    self.reachability?.whenUnreachable = {
      reachability in
      TXLogWarn("建立连接__失败")
      ReachabilitySource.notifyAll(type: ReachabilityType.error)

    }
    
  }
  
  static func stopNotifier(){
    self.reachability?.stopNotifier()
    self.reachability = nil
  }
  
}
